package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqcsMachineTestService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-13
 */
@Service
public class JqcsMachineTestServiceImpl extends ServiceImpl<JqcsMachineTestMapper, JqcsMachineTest> implements JqcsMachineTestService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JqcsSettingMapper jqcsSettingMapper;
    @Autowired
    JqzzMachineMapper jqzzMachineMapper;
    @Autowired
    JqzzSettingMapper jqzzSettingMapper;
    @Autowired
    JqcsSettingProcessMapper jqcsSettingProcessMapper;
    @Autowired
    JqcsMachineTestProecssMapper jqcsMachineTestProecssMapper;
    @Override
    public Boolean updateJqcsMachineTest(JqcsMachineTest jqcsMachineTest, Token token) {
        //待办修改
        if (jqcsMachineTest.getTaskstate()==0 && jqcsMachineTest.getTaskfinishstate()==0 &&
                "0".equals(jqcsMachineTest.getIstaskstart()) &&  "0".equals(jqcsMachineTest.getIstaskbegin()) &&
                "0".equals(jqcsMachineTest.getIstaskend())  && "0".equals(jqcsMachineTest.getIsreviewtask())
        ){
            jqcsMachineTest.setTaskreleaseperson(token.getUser().getId().toString());
            jqcsMachineTest.setTaskreleaseaddtime(new Date());

            return  super.updateById(jqcsMachineTest);
        }
        //接收任务
        if (jqcsMachineTest.getTaskstate()==0 && jqcsMachineTest.getTaskfinishstate()==0 &&
                "1".equals(jqcsMachineTest.getIstaskstart()) &&  "0".equals(jqcsMachineTest.getIstaskbegin()) &&
                "0".equals(jqcsMachineTest.getIstaskend())  && "0".equals(jqcsMachineTest.getIsreviewtask())
        ){
            jqcsMachineTest.setIstaskstart("1");
            jqcsMachineTest.setTaskstartaddtime(new Date());
            jqcsMachineTest.setTaskstate(1);
            return  super.updateById(jqcsMachineTest);
        }
        //开始任务
        if (jqcsMachineTest.getTaskstate()==1 && jqcsMachineTest.getTaskfinishstate()==0 &&
                "1".equals(jqcsMachineTest.getIstaskstart()) &&  "1".equals(jqcsMachineTest.getIstaskbegin()) &&
                "0".equals(jqcsMachineTest.getIstaskend())  && "0".equals(jqcsMachineTest.getIsreviewtask())
        ){
            jqcsMachineTest.setIstaskbegin("1");
            jqcsMachineTest.setTaskbeginaddtime(new Date());
            return super.updateById(jqcsMachineTest);
        }
        //结束任务
        if (jqcsMachineTest.getTaskstate()==1 && jqcsMachineTest.getTaskfinishstate()==0 &&
                "1".equals(jqcsMachineTest.getIstaskstart()) &&  "1".equals(jqcsMachineTest.getIstaskbegin()) &&
                "1".equals(jqcsMachineTest.getIstaskend())  && "0".equals(jqcsMachineTest.getIsreviewtask())
        ){
            jqcsMachineTest.setIstaskend("1");
            jqcsMachineTest.setTaskfinishstate(1);
            jqcsMachineTest.setTaskendaddtime(new Date());
            //生成打包记录
            return super.updateById(jqcsMachineTest);
        }
        //复核任务
        if (jqcsMachineTest.getTaskstate()==1 && jqcsMachineTest.getTaskfinishstate()==1 &&
                "1".equals(jqcsMachineTest.getIstaskstart()) &&  "1".equals(jqcsMachineTest.getIstaskbegin()) &&
                "1".equals(jqcsMachineTest.getIstaskend())  && "1".equals(jqcsMachineTest.getIsreviewtask())
        ){
            jqcsMachineTest.setIsreviewtask("1");
            jqcsMachineTest.setReviewaddtime(new Date());
            jqcsMachineTest.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqcsMachineTest);
        }

        return null;
    }

    @Override
    public Page<JqcsMachineTest> selectJqcsMachineTest(Page<JqcsMachineTest> page ,QueryJqcsMachineTest queryJqcsMachineTest) {
        Wrapper<JqcsMachineTest> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqcsMachineTest.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqcsMachineTest.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqcsMachineTest.getIsReviewTask());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqcsMachineTest.getCode(),null)){
            wrapper.like("TASKCODE",queryJqcsMachineTest.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqcsMachineTest.getName(),null)){
            wrapper.eq("RESOURCENAME",queryJqcsMachineTest.getName());
        }

            page = super.selectPage(page,wrapper);
        List<JqcsMachineTest> jqcsMachineTests = page.getRecords();
        for (JqcsMachineTest jqcsMachineTest : jqcsMachineTests) {
            if ( jqcsMachineTest.getTaskreleaseperson()!=null && !"".equals( jqcsMachineTest.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getTaskreleaseperson());
                jqcsMachineTest.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqcsMachineTest.getTaskperformperson()!=null && !"".equals( jqcsMachineTest.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getTaskperformperson());
                jqcsMachineTest.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqcsMachineTest.getReviewperson()!=null && !"".equals( jqcsMachineTest.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcsMachineTest.getReviewperson());
                jqcsMachineTest.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsMachineTest.getResourcename(),null)){
                JqcsSetting jqcsSetting = jqcsSettingMapper.selectById(jqcsMachineTest.getResourcename());
                jqcsMachineTest.setResourcenamename(jqcsSetting.getName());
            }
        }
     page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("97");
        return jyUsers;
    }

    @Override
    public boolean insertJqcsMachineTest(JqcsMachineTest jqcsMachineTest) throws AdscmSystemException {
        //
        String taskCode= jqcsMachineTest.getResourcename();
        //根据机器编码查询这个是否有这个总装
        Wrapper<JqzzMachine> jqzzMachineWrapper =new EntityWrapper<>();
        jqzzMachineWrapper.eq("TASKCODE",taskCode);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(jqzzMachineWrapper);
        if (!(jqzzMachines!=null && jqzzMachines.size()>0)){
            throw  new AdscmSystemException("不存在这样的机器编码，请核对");
        }
        Wrapper<JqcsMachineTest>  jqcsMachineTestWrapper=new EntityWrapper<>();
        jqcsMachineTestWrapper.eq("TASKCODE",taskCode);
        List<JqcsMachineTest> jqcsMachineTests = super.selectList(jqcsMachineTestWrapper);
        if (jqcsMachineTests!=null && jqcsMachineTests.size()>0){
            throw  new AdscmSystemException("存在机器编码的出厂检测记录");
        }
         //根据机型生成对应的检测记录
        JqzzMachine jqzzMachine = jqzzMachines.get(0);
        JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());
        Wrapper<JqcsSetting> jqcsSettingWrapper =new EntityWrapper<>();
        jqcsSettingWrapper.eq("type",jqzzSetting.getType());
        jqcsSettingWrapper.eq("isEnable",1);
        List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
        for (JqcsSetting jqcsSetting : jqcsSettings) {
            jqcsMachineTest.setBatchnums(jqzzMachine.getBatchnums());
            jqcsMachineTest.setTypenums(jqzzMachine.getTypenums());
            jqcsMachineTest.setTaskcode(taskCode);
            jqcsMachineTest.setResourcename(jqcsSetting.getId().toString());
            jqcsMachineTest.setTaskreleaseaddtime(new Date());
            jqcsMachineTest.setTaskfinishstate(0);
            jqcsMachineTest.setTaskstate(0);
            jqcsMachineTest.setIstaskstart("0");
            jqcsMachineTest.setIstaskbegin("0");
            jqcsMachineTest.setIstaskend("0");
            jqcsMachineTest.setIsreviewtask("0");
            jqcsMachineTest.setReviewresult("-1");
            super.insert(jqcsMachineTest);
            //查询这个记录对应的过程记录设置
            Wrapper<JqcsSettingProcess> jqcsSettingProcessWrapper=new EntityWrapper<>();
            jqcsSettingProcessWrapper.eq("pid" ,jqcsSetting.getId());
            List<JqcsSettingProcess> jqcsSettingProcesses = jqcsSettingProcessMapper.selectList(jqcsSettingProcessWrapper);
            for (JqcsSettingProcess jqcsSettingProcess : jqcsSettingProcesses) {
                JqcsMachineTestProecss jqcsMachineTestProecss =new JqcsMachineTestProecss();
                jqcsMachineTestProecss.setPid(jqcsMachineTest.getTid());
                jqcsMachineTestProecss.setName(jqcsSettingProcess.getName());
                jqcsMachineTestProecss.setCid(jqcsSettingProcess.getId());
                jqcsMachineTestProecss.setReference(jqcsSettingProcess.getReference());
                jqcsMachineTestProecss.setRemark(jqcsSettingProcess.getRemark());
                jqcsMachineTestProecss.setRank(jqcsSettingProcess.getRank());
                jqcsMachineTestProecss.setType(jqcsSettingProcess.getType());
                jqcsMachineTestProecss.setUnit(jqcsSettingProcess.getUnit());
                jqcsMachineTestProecssMapper.insert(jqcsMachineTestProecss);
            }

        }
        return true;
    }

    @Override
    public List<JqzzMachine> selectResource() {
        Wrapper<JqzzMachine> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",1);
        wrapper.eq("TASKFINISHSTATE",1);
        wrapper.eq("state",1);
        wrapper.eq("ISOUTSTORAGE",0);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(wrapper);
        return null;
    }
}
